package com.xinyue.dz.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.xinyue.dz.bean.Group;
import com.xinyue.dz.service.GroupService;

import commons.MessageUtil;
import commons.PageHandler;
import commons.ParamHandler;
import commons.data.Page;

@Controller
public class GroupController {
	@Autowired
	GroupService service;

	/**
	 * 查询数据并返回index页面
	 */
	@RequestMapping(value="/security/group/index.do")
	public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("/user/group_index");
		
		try {
			long total = service.queryCount();
			Page p = PageHandler.process(request, total);
			
			List<Group> list = service.queryAll(p.getBegin(), p.getCount());
			request.setAttribute("list", list);
		} catch (Exception e) {
			request.setAttribute("result", "查询数据失败，请稍后再试！");
		}
		
		//抓取操作信息
		MessageUtil.consumer(request);
		
		return mv;
	}
	
	/**
	 * 保存数据/修改数据
	 */
	@RequestMapping(value="/security/group/save.do")
	public ModelAndView save(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("redirect:/security/group/index.do");
		
		try {
			Group o = ParamHandler.process(request, Group.class);
			service.saveOrUpdate(o);
			
			MessageUtil.success(request.getSession(), "数据操作成功！");
		} catch (Exception e) {
			MessageUtil.error(request.getSession(), "数据操作失败，请稍后再试！");
		}
		
		return mv;
	}
	
	/**
	 * 删除数据
	 */
	@RequestMapping(value="/security/group/delete.do")
	public ModelAndView delete(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("forward:/security/group/index.do");
		
		String id = request.getParameter("groupId");
		try {
			service.deleteById(Integer.valueOf(id));
			MessageUtil.success(request, "删除数据成功！");
		} catch (Exception e) {
			MessageUtil.error(request, "删除数据失败，请稍后再试！");
		}
		
		return mv;
	}
	
	/**
	 * 数据新建或者修改页面
	 */
	@RequestMapping(value="/security/group/build.do")
	public ModelAndView build(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("/user/group_build");
		
		String id = request.getParameter("groupId");
		try {
			Group obj = service.queryById(Integer.valueOf(id));
			request.setAttribute("obj", obj);
		} catch (Exception e) {
			request.setAttribute("error", "加载数据失败，请稍后再试！");
		}
		
		return mv;
	}
	
	/**
	 * 数据查看页面
	 */
	@RequestMapping(value="/security/group/look.do")
	public ModelAndView look(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("/user/group_look");
		
		String id = request.getParameter("groupId");
		try {
			Group obj = service.queryById(Integer.valueOf(id));
			request.setAttribute("obj", obj);
		} catch (Exception e) {
			request.setAttribute("error", "加载数据失败，请稍后再试！");
		}
		
		return mv;
	}
}